package cn.elwy.rcp.kit.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.ExpandAdapter;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;

public class ExpandDialog extends Dialog {

  protected Object result;
  protected Shell shell;

  /**
   * Create the dialog.
   * @param parent
   * @param style
   */
  public ExpandDialog(Shell parent, int style) {
    super(parent, style);
    setText("SWT Dialog");
  }

  /**
   * Open the dialog.
   * @return the result
   */
  public Object open() {
    createContents();
    shell.open();
    shell.layout();
    Display display = getParent().getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    return result;
  }

  /**
   * Create contents of the dialog.
   */
  private void createContents() {
    shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER);
    shell.setSize(450, 300);
    shell.setText(getText());
    shell.setLayout(new FormLayout());

    final ExpandBar expandBar = new ExpandBar(shell, SWT.V_SCROLL);
    FormData fd_expandBar = new FormData();
    fd_expandBar.bottom = new FormAttachment(100);
    fd_expandBar.right = new FormAttachment(100);
    fd_expandBar.top = new FormAttachment(0);
    fd_expandBar.left = new FormAttachment(0);
    expandBar.setLayoutData(fd_expandBar);
    expandBar.addExpandListener(new ExpandAdapter() {
      public void itemExpanded(final ExpandEvent e) {
        ExpandBar expandBar = (ExpandBar) e.getSource();
        ExpandItem actionItem = (ExpandItem) e.item;
        ExpandItem[] items = expandBar.getItems();
        ExpandItem item;
        int headHeight = 0;
        for (int i = 0, j = items.length; i < j; i++) {
          item = items[i];
          if (item.getExpanded()) {
            item.setExpanded(false);

          }
          headHeight += item.getHeaderHeight();

        }
        actionItem.setHeight(expandBar.getSize().y - headHeight - actionItem.getHeaderHeight());
      }
    });
    expandBar.addControlListener(new ControlListener() {

      public void controlMoved(ControlEvent e) {
      }

      public void controlResized(ControlEvent e) {
        ExpandItem item = null;
        int headHeight = 0;
        for (int i = 0; i < expandBar.getItemCount(); i++) {
          if (expandBar.getItem(i).getExpanded()) {
            item = expandBar.getItem(i);
          }
          headHeight += expandBar.getItem(i).getHeaderHeight();
        }
        if (item != null) {
          item.setHeight(expandBar.getSize().y - headHeight - item.getHeaderHeight());
        }
      }
    });
    expandBar.setRedraw(true);
    expandBar.setLayoutDeferred(true);
    // long start = System.currentTimeMillis();
    // List modules = ClientDocumentService.getModules();
    // logger.debug("getmodules usered time is :" + (System.currentTimeMillis() - start));
    // if (modules != null && modules.size() > 0) {
    // final ExpandItem[] newItemExpandItem = new ExpandItem[modules.size()];
    // trees = new Tree[modules.size()];
    // Module module;
    // List children;
    for (int i = 0; i < 3; i++) {
      // formatModAttribute(module);
      ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);

      expandItem.setText("expandItem" + i);
      Tree trees = new Tree(expandBar, SWT.BORDER);
      // trees[i].setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));
      // trees[i].setFont(SWTResourceManager.getFont("", 10,
      // SWT.BOLD));
      // newItemExpandItem[i].setHeight(trees[i].computeSize(SWT.DEFAULT
      // , SWT.DEFAULT).y);
      expandItem.setControl(trees);
      // if (i == 0) {// || i == modules.size() - 1) {
      // // 设置第一个节点
      // setFirstTree(trees[i], newItemExpandItem[i]);
      // } else {
      // // newItemExpandItem[i].setExpanded(false);
      // }
      // children = module.getChildList();
      // List childrenOrder = new ArrayList();
      //
      // if (children != null) {
      // for (int j = children.size() - 1; j >= 0; j--) {
      // childrenOrder.add(children.get(j));
      //
      // }
      // newTree(childrenOrder, trees[i]);
      // }
      //
      // // newItemExpandItem[i].setHeight(getTreeHeight(trees[i]));
      // final Tree tempTree = trees[i];
      // final ExpandItem tempItem = newItemExpandItem[i];
      // if (tempItem.getExpanded()) {
      // tempItem.setHeight(expandBar.getSize().y);
      // }

      // trees[i].addTreeListener(new TreeListener() {
      //
      // public void treeCollapsed(TreeEvent e) {
      // try {
      // ((TreeItem) e.item).setImage(folder);
      // } catch (Exception ex) {
      // ex.printStackTrace();
      // }
      // Display.getDefault().timerExec(10, new Runnable() {
      // public void run() {
      // int height = getTreeHeight(tempTree);
      //
      // Point p = tempTree.computeSize(SWT.DEFAULT, height);
      //
      // tempItem.setHeight(p.y);
      // }
      // });
      // }
      //
      // public void treeExpanded(TreeEvent e) {
      // ((TreeItem) e.item).setImage(folder_open);
      // // 展开当前节点，关闭同级节点
      // TreeItem item = (TreeItem) e.item;
      // if (item.getParentItem() != null) {
      // for (TreeItem br : item.getParentItem().getItems()) {
      // if (br != item) {
      // br.setExpanded(false);
      // }
      // }
      // } else {
      // TreeItem[] items = ((Tree) e.getSource()).getItems();
      // for (TreeItem firstLevel : items) {
      // if (firstLevel != item && firstLevel.getItemCount() > 0) {
      // firstLevel.setExpanded(false);
      // }
      // }
      // }
      //
      // }
      // });
    }
    //
    // } else {
    // // MessageDialog.openInformation(Display.getDefault().getActiveShell(), "系统消息", "系统未给当前角色设置权限,请和管理员联系");
    // }

  }

}
